package com.flowabledemo.service.impl;

import com.dto.PageVo;
import com.flowabledemo.dao.TestUserDao;
import com.flowabledemo.dto.TestUserDto;
import com.flowabledemo.dto.TestUserPageDto;
import com.flowabledemo.entity.TestUser;
import com.flowabledemo.service.TestUserService;
import com.flowabledemo.vo.TestUserVo;
import com.util.BeanExtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;

/**
* @author :zhoushunquan
* @description: 用户模板服务接口实现类
* @create date: 2022-02-24 11:23:36
*/

@Slf4j
@Service
public class TestUserServiceImpl implements TestUserService {

    @Autowired
    TestUserDao testUserDao;

    /**
    * 保存用户模板
    * @param testUserDto
    * @return
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(TestUserDto testUserDto) {
        return testUserDao.add(copyToTestUser(testUserDto));
    }

    /**
    * 根据ID更新用户模板
    * @param testUserDto
    * @return
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(TestUserDto testUserDto) {
        return testUserDao.update(copyToTestUser(testUserDto));
    }

    /**
    * 删除用户模板
    * @param id
    * @return
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Integer id) {
        return testUserDao.del(id);
    }

    /**
    * 根据查询用户模板
    * @param id
    * @return
    */
    @Override
    public TestUserVo get(Integer id) {
        TestUser testUser= testUserDao.get(id);
        return copyToTestUserVo(testUser);
    }

    /**
    * 查询用户模板
    * @param testUserDto
    * @return
    */
    @Override
    public List<TestUserVo> findList(TestUserDto testUserDto) {
        List<TestUser> list= testUserDao.findList(copyToTestUser(testUserDto));
        return BeanExtUtil.copyList(list,TestUserVo.class);
    }

    /**
    * 查询用户模板分页信息
    * @param testUserPageDto
    * @return PageVO
    */
    @Override
    public PageVo findPageList(TestUserPageDto testUserPageDto) {
        Page<TestUser> page=new Page<>(testUserPageDto.getPageNum(),testUserPageDto.getPageSize());
        TestUser testUser = BeanExtUtil.copyTo(testUserPageDto, TestUser.class);
        testUserDao.findPageList(page ,testUser);
        return BeanExtUtil.copyTo(page, PageVo.class);
    }

    private TestUser copyToTestUser(TestUserDto testUserDto) {
       return BeanExtUtil.copyTo(testUserDto, TestUser.class);
    }

    private TestUserVo copyToTestUserVo(TestUser testUser) {
        return BeanExtUtil.copyTo(testUser, TestUserVo.class);
    }
}
